var _sampleDataNodeArray;
var _sampleDataXml ;
var _regionalOptionsDataXml ;
var _regionalOptionsIndexXml ;
var _regionalOptionsUpdatedSampleStandard ;
var _defaultSampleDataArray ;
var _defaultLCID ;
var _lastSelectedLCID ;

window.attachEvent('onload', Initialize);

function Initialize()
{
InitializeRegionalOptions();


var lcid = document.getElementById("selectRegionalFormatCode");
if ( !IsNull(lcid))
{
_defaultLCID = lcid.value ;
_lastSelectedLCID = _defaultLCID ;
}


_defaultSampleDataArray = new Array();
for (var elementId in _sampleDataNodeArray )
{
var element = document.getElementById(elementId);
if ( !IsNull(element))
{
_defaultSampleDataArray[elementId] = element.value;
}
}
}

function InitializeRegionalOptions()
{
LoadLocaleDefaultDataXml(true,LoadSampleDataXmlHandler);

_regionalOptionsDataXml = "" ;

_sampleDataNodeArray= new Array();
_sampleDataNodeArray["sample_number_d"] = "number" ;
_sampleDataNodeArray["sample_currency_d"] = "currency" ;
_sampleDataNodeArray["sample_time_d"] = "time" ;
_sampleDataNodeArray["sample_shortdate_d"] = "shortdate" ;
_sampleDataNodeArray["sample_longdate_d"] = "longdate" ;
}

function LoadLocaleDefaultDataXml(bAsync,oCallback)
{
var oRemoteCmd = new RemoteCommand("RegionalOptions","GetDefaultsForAllLocales");
oRemoteCmd.SetParameter("locale","all");

if ( bAsync )
{
oRemoteCmd.Execute(oCallback);
}
else
{
var oResult = oRemoteCmd.Execute();
if(oResult.Success == ERROR_NONE)
{
return (!IsNull(oCallback) && typeof(oCallback)=="function") ? oCallback(oResult) : oResult.Xml.text;
}
return null ;
}
}

function LoadSampleDataXmlHandler(oResult)
{
if(oResult.Success == ERROR_NONE)
{
var oResultXml = oResult.Xml ;
if(!IsNull(oResultXml) && oResultXml.text.length > 0)
{
_sampleDataXml = CrmEncodeDecode.CrmXmlDecode(oResultXml.text);
}
}
}

function onRegionalFormatChange(o)
{
var selectedLocale = o.value ;

_lastSelectedLCID = selectedLocale ;


_regionalOptionsDataXml = "" ;
_regionalOptionsIndexXml = "" ;
_regionalOptionsUpdatedSampleStandard = "" ;


if ( selectedLocale == _defaultLCID )
{
for (var elementId in _defaultSampleDataArray )
{
var element = document.getElementById(elementId);
if ( !IsNull(element))
{
element.value = _defaultSampleDataArray[elementId];
}
}

return ;
}

if ( IsNull(_sampleDataXml))
{
LoadLocaleDefaultDataXml(false,LoadSampleDataXmlHandler);
}


var oXmlDoc = CreateXmlDocument(false);
oXmlDoc.loadXML(_sampleDataXml);

var localeDefaultData = oXmlDoc.selectSingleNode("sampledata/lcid_"+selectedLocale);

for (var elementId in _sampleDataNodeArray )
{
var element = document.getElementById(elementId);
if ( !IsNull(element))
{
element.value = localeDefaultData.selectSingleNode(_sampleDataNodeArray[elementId]).text;
}
}
}

function CustomizeRegionalOptionsClicked(oButton,sUserSettings)
{
var sUrl = prependOrgName("/Tools/RegionalOptions/dialogs/regionaloptions.aspx") ;

sUrl += "?usersettings=" + CrmEncodeDecode.CrmUrlEncode(sUserSettings) ;

if ( sUserSettings.toLowerCase() != "false" )
{
sUrl += "&lcid=" + CrmEncodeDecode.CrmUrlEncode(selectRegionalFormatCode.value) ;
}
else
{
sUrl += "&lcid=" + CrmEncodeDecode.CrmUrlEncode(crmForm.selectRegionalFormatCode.value) ;
}

var lcid = document.getElementById("selectRegionalFormatCode");


var oArgs = new Array();
if ( lcid.value == _lastSelectedLCID )
{
oArgs["sLocaleSettingsXml"] = _regionalOptionsIndexXml;
oArgs["sSampleDataXml"] = _regionalOptionsUpdatedSampleStandard;
}

var retval = openStdDlg(sUrl , oArgs, 600, 500);

if (!IsNull(retval))
{
_regionalOptionsDataXml = retval["localesettings"];
_regionalOptionsIndexXml = retval["localesettingsindex"];
_regionalOptionsUpdatedSampleStandard = retval["sampledata"];

if ( !IsNull(_regionalOptionsUpdatedSampleStandard) && _regionalOptionsUpdatedSampleStandard != "")
{
var oXmlDoc = CreateXmlDocument(false);
oXmlDoc.loadXML(_regionalOptionsUpdatedSampleStandard);

var selectPrefix = "sampledata/lcid_"+lcid.value+"/" ;


for (var elementId in _sampleDataNodeArray )
{
var element = document.getElementById(elementId);
if ( !IsNull(element))
{
element.value = oXmlDoc.selectSingleNode(selectPrefix+_sampleDataNodeArray[elementId]).text;
}
}
}
}
}

function GetRegionalOptionsDataXml()
{
return _regionalOptionsDataXml ;
}